<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>颜色切换-v-bind</title>
</head>

<body>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

  <div id="Application">
    <div class="demo">
      <button v-for="(item, index) in btns" :key="index" @click="onBtnClick(item.bgColor, item.textColor)">
        {{ item.title }}
      </button>

      <div>
        <div class="example" :style="styleVar">Hello World</div>
      </div>
    </div>
  </div>
  <script>
    const App = Vue.createApp({
      setup() {
        const currentBgColor = Vue.ref("#333333");
        const currentTextColor = Vue.ref("#FFFFFF");

        const btns = [
          { title: "红色主题", bgColor: "#FF9191", textColor: "#FF0000" },
          { title: "蓝色主题", bgColor: "#B3C4FF", textColor: "#042BA9" },
          { title: "默认主题", bgColor: "#333333", textColor: "#FFFFFF" },
        ];
        const onBtnClick = (bgColor, textColor) => {
          console.log(bgColor, textColor);
          currentBgColor.value = bgColor;
          currentTextColor.value = textColor;
        };
        const styleVar = Vue.computed(() => {
          return {
            "--textColor": currentTextColor.value,
            "--bgColor": currentBgColor.value,
          };
        });

        return {
          btns,
          onBtnClick,
          styleVar,
        };
      },
    });
    App.mount("#Application");
  </script>
  <style scoped lang="scss">
    .demo {
      padding: 10px;
    }

    .demo .example {
      display: inline-block;
      margin-top: 20px;
      font-size: 20px;
      padding: 20px 50px;
      color: var(--textColor);
      background: var(--bgColor);
    }
  </style>
</body>

</html>